//
//  MCMineHeaderView.m
//  TLYL
//
//  Created by MC on 2017/6/12.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCMineHeaderView.h"
#define WIDTH_BTN  45
@interface MCMineHeaderView()


/*
 * 打底一
 */
@property (nonatomic,strong)UIView * view_up;

//用户头像
@property (nonatomic,strong)UIImageView * imgV_UserIcon;

//用户名
@property (nonatomic,strong)UILabel * lab_UserName;


//充值
@property (nonatomic,strong)UIButton * btn_recharge;
//提款
@property (nonatomic,strong)UIButton * btn_withdraw;
//站内信
@property (nonatomic,strong)UIButton * btn_message;


/*
 * 打底二
 */
@property (nonatomic,strong)UIView * view_down;
//可用余额
@property (nonatomic,strong)UILabel *lab_accountBalance;

//冻结金额
@property (nonatomic,strong)UILabel *lab_frozenAccount;



@end

@implementation MCMineHeaderView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    
    
    self.backgroundColor=[UIColor whiteColor];
    
    /*
     * 上半部分视图
     */
    _view_up=[[UIView alloc]init];
    [self addSubview:_view_up];
    _view_up.backgroundColor=RGB(75, 166, 226);
    _view_up.frame=CGRectMake(0, 20, G_SCREENWIDTH, 147);
    
    /*
     * 打底二
     */
    _view_down=[[UIView alloc]init];
    [self addSubview:_view_down];
    _view_down.backgroundColor=RGB(96, 186, 227);
    _view_down.frame=CGRectMake(0, 147+20, G_SCREENWIDTH, 33);
    
    /**
     * 设置按钮
     **/
    _imgV_setting=[[UIImageView alloc]init];
    [_view_up addSubview:_imgV_setting];
    _imgV_setting.userInteractionEnabled=YES;
    _imgV_setting.image=[UIImage imageNamed:@"MC_My_Setting"];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_setting)];
    [_imgV_setting addGestureRecognizer:tap];
    
    /*
     * 用户头像
     */
    _imgV_UserIcon=[[UIImageView alloc]init];
    _imgV_UserIcon.layer.cornerRadius=75/2.0;
    _imgV_UserIcon.clipsToBounds=YES;
    [_view_up addSubview:_imgV_UserIcon];
    _imgV_UserIcon.userInteractionEnabled=YES;
    _imgV_UserIcon.image=[UIImage imageNamed:@"fc_sd"];
    _imgV_UserIcon.frame=CGRectMake(23, 35, 75, 75);
    
    /*
     * 用户名
     */
    _lab_UserName=[[UILabel alloc]init];
    _lab_UserName.font = [UIFont systemFontOfSize:12];
    _lab_UserName.numberOfLines=1;
    _lab_UserName.text = @"加载中...";
    _lab_UserName.textColor=[UIColor whiteColor];
    _lab_UserName.textAlignment=NSTextAlignmentCenter;
    [_view_up addSubview:_lab_UserName];

    [_lab_UserName mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(_imgV_UserIcon.mas_centerX);
        make.top.equalTo(_imgV_UserIcon.mas_bottom).offset(7);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(220);
    }];


    //充值
    _btn_recharge=[[UIButton alloc]init];
    [self setBtn:_btn_recharge andTitle:@"充值" andImgName:@"fc_sd" andIndex:0];
    
    
    //提款
    _btn_withdraw=[[UIButton alloc]init];
    [self setBtn:_btn_withdraw andTitle:@"提款" andImgName:@"fc_sd" andIndex:1];

    //站内信
    _btn_message=[[UIButton alloc]init];
    [self setBtn:_btn_message andTitle:@"站内信" andImgName:@"fc_sd" andIndex:2];

    
    
    
    
/******************************************/
    

    /*
     * 可用余额
     */
    _lab_accountBalance=[[UILabel alloc]init];
    _lab_accountBalance.font = [UIFont systemFontOfSize:12];
    _lab_accountBalance.numberOfLines=1;
    _lab_accountBalance.textColor=[UIColor whiteColor];
    _lab_accountBalance.text = @"可用余额";
    _lab_accountBalance.textAlignment=NSTextAlignmentCenter;
    [_view_down addSubview:_lab_accountBalance];
    _lab_accountBalance.frame=CGRectMake(0, 0, G_SCREENWIDTH/2.0, 33);
    
    
    
    /*
     * 冻结金额
     */
    _lab_frozenAccount=[[UILabel alloc]init];
    _lab_frozenAccount.font = [UIFont systemFontOfSize:12];
    _lab_frozenAccount.numberOfLines=1;
    _lab_frozenAccount.text = @"冻结余额";
    _lab_frozenAccount.textColor=[UIColor whiteColor];
    _lab_frozenAccount.textAlignment=NSTextAlignmentCenter;
    [_view_down addSubview:_lab_frozenAccount];

    _lab_frozenAccount.frame=CGRectMake(G_SCREENWIDTH/2.0, 0, G_SCREENWIDTH/2.0, 33);
    
    
    
    
    [_btn_recharge addTarget:self action:@selector(action_recharge) forControlEvents:UIControlEventTouchUpInside];
    [_btn_withdraw addTarget:self action:@selector(action_withdraw) forControlEvents:UIControlEventTouchUpInside];
    [_btn_message addTarget:self action:@selector(action_message) forControlEvents:UIControlEventTouchUpInside];
    
    [self layOutConstraints];
    
}

-(void)setBtn:(UIButton *)btn andTitle:(NSString *)title andImgName:(NSString *)imgName andIndex:(int)index{
    //小球宽度

    CGFloat SPadding=(G_SCREENWIDTH-23-75-3*WIDTH_BTN)/35.0;
    
    CGFloat Padding=SPadding*8;//10+8+8+9
    
    CGFloat WIDTH_L=23+75+SPadding*10;
    
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    btn.backgroundColor=[UIColor orangeColor];
    [btn setTitle:title forState:UIControlStateNormal];
    [_view_up addSubview:btn];
    btn.imageView.layer.cornerRadius=35/2.0;
    btn.imageView.clipsToBounds=YES;
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(WIDTH_BTN);
        make.left.equalTo(_view_up.mas_left).offset(WIDTH_L+WIDTH_BTN*index+Padding*index);
        make.height.mas_equalTo(WIDTH_BTN+20);
        make.centerY.equalTo(_imgV_UserIcon.mas_centerY);
    }];
    [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
    btn.titleLabel.font=[UIFont systemFontOfSize: 12];
    [self setButtonContentCenter:btn];
}

-(void)layOutConstraints{

    /**
     * 设置按钮
     **/
    [_imgV_setting mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_view_up.mas_top).offset(10);
        make.right.equalTo(_view_up.mas_right).offset(-10);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];

    
}

/**
 *   获取字符宽度
 */
- (CGFloat)getWidthWithTitle:(NSString *)title font:(CGFloat )font {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1000, 0)];
    label.text = title;
    label.font = [UIFont systemFontOfSize:font];
    [label sizeToFit];
    return label.frame.size.width;
}
-(void)setDataSource:(id)dataSource{
    _dataSource=dataSource;
    _lab_UserName.text = @"Simone";

    _lab_accountBalance.text = @"可用余额:3000000元";
    _lab_frozenAccount.text = @"冻结余额:3000000元";
}

/*
 * 跳转设置
 */
-(void)action_setting{
    if ([self.delegate respondsToSelector:@selector(MCMineHeaderView_Setting)]) {
        [self.delegate MCMineHeaderView_Setting];
    }
}

/*
 * 跳转充值
 */
-(void)action_recharge{
    if ([self.delegate respondsToSelector:@selector(MCMineHeaderView_Recharge)]) {
        [self.delegate MCMineHeaderView_Recharge];
    }
}

/*
 * 跳转提现
 */
-(void)action_withdraw{
    if ([self.delegate respondsToSelector:@selector(MCMineHeaderView_Withdraw)]) {
        [self.delegate MCMineHeaderView_Withdraw];
    }
}

/*
 * 跳转站内信
 */
-(void)action_message{
    if ([self.delegate respondsToSelector:@selector(MCMineHeaderView_Message)]) {
        [self.delegate MCMineHeaderView_Message];
    }
}


-(void)setButtonContentCenter:(UIButton *)button
{
    CGSize imgViewSize,titleSize,btnSize;
    UIEdgeInsets imageViewEdge,titleEdge;
    

    CGFloat imgV_W=35;
    //设置按钮内边距
    imgViewSize = button.imageView.bounds.size;
    titleSize = button.titleLabel.bounds.size;
    btnSize = button.bounds.size;
    CGFloat L_Space=(btnSize.width-imgV_W)/2.0;
    CGFloat R_Space=L_Space;
    CGFloat H_Space = (btnSize.height/2.0-75/2.0)+75/2.0-imgV_W/2.0;
    CGFloat bottomSpace = btnSize.height-H_Space-imgV_W;
    

    imageViewEdge = UIEdgeInsetsMake(H_Space,L_Space, bottomSpace, R_Space);
    [button setImageEdgeInsets:imageViewEdge];
    titleEdge = UIEdgeInsetsMake(H_Space+imgV_W+5, - imgViewSize.width-5, 0.0, 0.0);
    [button setTitleEdgeInsets:titleEdge];
}
@end













































